<?php
include("functions.php");
include("libs/smarty.class.php");

$smarty = new Smarty();
$title = "Szczegóły praktyki.";
$smarty->assign("title",$title);

$practice_id = $_GET['id'];

if($_POST['send']){
    //CHECK LOGGED
    $practice_id = $_POST['practice_id'];
    if(isset($_SESSION['user_id'])){
        $student_id = $_SESSION['user_id'];
        //CHECK STUDENT
        if($_SESSION['user_type'] == 'user_student'){
            //CHECK PRACTICE AMOUNT
            $amount = getRow("amount", $practice_id, "practices");
            $amount = $amount['0']['amount'];
            if($amount > 0 ){
                $check = mysql_query("SELECT sp.id FROM student_practices sp WHERE sp.student_id ='$student_id'");
                if(mysql_num_rows($check) > 0 ){
                    //CHECK STATUS
                    $status = mysql_fetch_assoc(mysql_query("SELECT sp.status FROM student_practices sp WHERE sp.student_id ='$student_id'"));
                    $status = $status['status'];
                    switch($status){
                        case 0 : echo "praktyka nie potwierdzona";
                            $info_action = displayInfo(4,'red');
                            break;
                        case 1 : echo "w trakcie praktyki";
                            $info_action = displayInfo(5,'red');
                            break;
                        /*case 2 : echo "praktyka zakończona";
                            //PREVIOUSLY PRACTICE FINISHED, POSSIBLE TO START NEW ONE
                            break;
                        case -1: echo "praktyka odrzucona";
                            //PREVIOUSLY PRACTICE REFUSED, POSSIBLE TO START NEW ONE
                            break; */
                        case 2 || -1:
                             echo addStudentPractice($student_id,$practice_id);
                             echo changeAmountPractice($practice_id,-1);
                             $info_action = displayInfo(6,'green');
                             break;
                    }
                }else{
                    //ADD PRACTICE WITH STATUS 0 (Waiting to accept)
                }
                /*
                //CHECK STUDENT PRACTICE
                $practice = getRow("practice_id", $student_id,"user_student");

                //CHECK PRACTICE QUEUE
                $check = mysql_query("SELECT spq.id FROM student_practices_queue spq WHERE spq.student_id ='$student_id'");
                if(mysql_num_rows($check) == 0){
                    //ADD TO QUEUE
                    $student_practice_id = $practice['0']['practice_id'];
                    if($student_practice_id == 0){
                        $query = mysql_query("INSERT INTO `student_practices_queue` (`practice_id`,`student_id`)
                            VALUES ('$practice_id','$student_id')");
                        $amount-=1;
                        $query2 = mysql_query("UPDATE `practices` SET amount = '$amount'  WHERE id = '$practice_id'");
                        $info_action = displayInfo(6,'green');
                    }else{
                        $info_action = displayInfo(5,'red');
                    }
                }else{
                   $info_action = displayInfo(4,'red');
                }
                */
            }else{
                $info_action = displayInfo(3,'red');
            }
        }else{
            $info_action = displayInfo(2,'red');
        }
    }else{
         $info_action = displayInfo(1,'red');
    }
}


if($practice_id!=''){
    $practiceDetails = getPracticeDetails($practice_id);
    $smarty->assign("p",$practiceDetails['practice']);
    $smarty->assign("c",$practiceDetails['company']);
    $smarty->assign("pe",$practiceDetails['person']);
    $smarty->assign("practiceDetails",$practiceDetails);
}

$smarty->assign("info_action",$info_action);
$smarty->display('practice_details.tpl');
?>
